//
//  FxBaseController.m
//  NewsReader
//
//  Created by zhouxiaobo on 15/11/1.
//  Copyright © 2015年 mapath. All rights reserved.
//

#import "FxBaseController.h"
#import "FxActivityIndicator.h"

@implementation FxBaseController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavBarImage];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)opFail:(NSString *)errorMessage {
    BASE_ERROR_FUN(errorMessage);
    [self showIndicator:errorMessage autoHide:YES afterDelay:YES];
}

- (void)opSuccess:(id)data {
    [self hideIndicator];
}

- (void)setNavBarImage {
    UIImage *image = [UIImage imageNamed:[FxGlobal isSystemLowIOS7]?@"NavigationBar44.png":@"NavigationBar64.png"];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
    NSDictionary *attr = @{
                           NSForegroundColorAttributeName:[UIColor whiteColor],
                           NSFontAttributeName:[UIFont systemFontOfSize:18]
                           };
    [self.navigationController.navigationBar setTitleTextAttributes:attr];
}

- (UIButton *)customButton:(NSString *)imageName selector:(SEL)sel{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 44, 44);
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    return button;
}


- (void)setNavigationLeft:(NSString *)imageName {
    //UIImage *image = [UIImage imageNamed:imageName];
    //UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:nil];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:[self customButton:imageName selector:nil]];
    
    self.navigationItem.leftBarButtonItem = item;
}

- (void)setNavigationRight:(NSString *)imageName {
//    UIImage *image = [UIImage imageNamed:imageName];
//    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:nil];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:[self customButton:imageName selector:nil]];
    self.navigationItem.rightBarButtonItem = item;
}

- (void)setNavigationTitleImage:(NSString *) imagePath {
    UIImage *img = [UIImage imageNamed:imagePath];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:img];
    self.navigationItem.titleView = imageView;
}

#pragma mark - Activity methods

- (FxActivity *)showActivityInView:(UIView *)view {
    FxActivity * activity = [[FxActivityIndicator alloc] initWithView:view];
    CGRect frame = view.bounds;
    
    activity.frame = frame;
    [view addSubview:activity];
    activity.labelText = @"";
    
    return activity;
}

- (void)showIndicator:(NSString *)tipMessage
             autoHide:(BOOL)hide
           afterDelay:(BOOL)delay {
    if (_activity == nil) {
        _activity = [self showActivityInView:self.view];
    }
    
    if (tipMessage != nil) {
        _activity.labelText = tipMessage;
        [_activity show:NO];
    }
    
    if (hide && _activity.alpha>=1.0) {
        if (delay)
            [_activity hide:YES afterDelay:AnimationSecond];
        else
            [_activity hide:YES];
    }
}

- (void)hideIndicator {
    [_activity hide:YES];
}


@end
